package com.rzico.sbl.ui.mine

import android.graphics.drawable.Drawable
import android.view.ViewTreeObserver
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.utils.PictureFileUtils
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityShopCodeBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.helper.BitmapHelper
import com.xinnuo.common.helper.BitmapHelper.getBitmap
import com.xinnuo.common.helper.getString
import com.xinnuo.common.helper.longToast
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.glideExt._DrawableImageViewTarget
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import io.reactivex.Flowable

class ShopCodeActivity : BaseActivity(R.layout.activity_shop_code),
    ViewTreeObserver.OnGlobalLayoutListener {

    private val mBinding by lazy { ActivityShopCodeBinding.bind(getRootView()) }
    private var mImageHeight = 0

    override fun getViewModel() = get<CommonViewModel>()

    override fun initData() {
        initTitle(title = "店铺二维码")

        Glide.with(this)
            .load("${BaseUrl.shopCode}?uid=${getString("uid")}")
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(
                object : _DrawableImageViewTarget(mBinding.codeImg) {
                    override fun onLoadStarted(placeholder: Drawable?) {
                        super.onLoadStarted(placeholder)
                        showLoading()
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        super.onLoadFailed(errorDrawable)
                        dismissLoading()
                    }

                    override fun setResource(resource: Drawable?) {
                        super.setResource(resource)
                        dismissLoading()
                    }
                }
            )
    }

    override fun initListener() {
        mBinding
            .codeImg
            .viewTreeObserver
            .addOnGlobalLayoutListener(this)

        mBinding.btAdd.oneClick {
            if (mImageHeight > 0) {
                Flowable.just(mBinding.codeImg)
                    .map {
                        return@map BitmapHelper.saveBitmap(
                            baseContext,
                            it.getBitmap(),
                            PictureFileUtils.createCameraFile(
                                baseContext,
                                SelectMimeType.ofImage(),
                                "", "", ""
                            )!!
                        )
                    }
                    .compose(applyFlowableSchedulers())
                    .doOnSubscribe { showLoading() }
                    .doFinally { dismissLoading() }
                    .subscribeByFinally(
                        onSuccess = { longToast(it) }
                    )
            }
        }
    }

    override fun onGlobalLayout() {
        mImageHeight = mBinding.codeImg.measuredHeight
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding
            .codeImg
            .viewTreeObserver
            .removeOnGlobalLayoutListener(this)
    }

}